package com.breakfast.fun.ui.user.login;

import com.breakfast.fun.Constant;
import com.breakfast.fun.data.entity.user.coupon.Coupon;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.T;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * http://beta.52letsgo.cn/m/Coupon/registerGift?mobile=18359928890
 * Created by Administrator on 2017/2/6.
 */


public class GiftPresenter extends GiftContract.Presenter {
    @Override
    public void getGift() {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("mobile", Constant.getUserData().getMobile());

        showLoadingDialog(true);

        rxManager.add(model.getGiftModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Coupon>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("发送失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Coupon coupon) {
                        hideLoadingDialog();
                        if (coupon.isSuccess()) {
                            view.showGift(coupon);
                        } else {
                            T.show(coupon.getMsg());
                            view.closeActivity();
                        }
                    }

                }));
    }


    @Override
    public void onStart() {
        getGift();
    }
}
